layui.use(['form'], function() {
    var form = layui.form
        , $ = layui.$;
    //通过session获取修改类型和键值
    let courseData = JSON.parse(sessionStorage.getItem('courseData'));
    let user = JSON.parse(sessionStorage.getItem('user'));
    selectTime($, courseData);
    form.on('select(time)', function(data){
        if(data != null){
            selectSlot($, data.value, courseData);
        }
    })
    //监听提交
    form.on('submit(formDemo)', function(data){
        $.ajax({
            type: 'post',
            url: '/Class/apply/add',
            dataType: 'json',
            data: {
                "userId": user.id,
                "type": parseInt(2),
                "typeData": data.field.createTime + '~' + data.field.timeSlot,
                "startTime": new Date(),
                "state": parseInt(1),
            },
            success: (res) =>{
                if(res.code === 0){
                    layer.msg(res.msg);
                    sessionStorage.setItem('courseData', null);
                    let indexs = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(indexs);
                    window.parent.location.reload();
                }else layer.msg(res.msg);
            }
        })
        return false;
    });
})
//设置时间选项， 两周内
function selectTime($, courseData) {
    let time = new Date(courseData.createTime);
    let i = 0;
    while (i <= 14){
        i++;
        let val = utils.formatDate(time.setDate(time.getDate() + 1));
        $("#createTime").append("<option value=\"" + val + "\">" + val + "</option>");
    }
    layui.form.render("select");
}
//根据时间来添加节数，无冲突
function selectSlot($, time, courseData) {
    $.ajax({
        type: 'post',
        url: '/Class/table/selectSlot',
        dataType: 'json',
        data: { time, "table": courseData},
        success: (res) =>{
            if(res.code === 0){
                let data = res.data;
                for (let i = 0; i < data.length; i++){
                    $('#timeSlot').append("<option value=\"" + data[i].id + "\">" + '第'+data[i].node+'节' + "</option>");
                }
            }
        }
    })
}